/**
 * Created with JetBrains WebStorm.
 * User: Administrator
 * Date: 6/6/12
 * Time: 12:11 AM
 * To change this template use File | Settings | File Templates.
 */

const X_VALUE = 0;
const Y_VALUE = 1;
const EMPTY_VALUE = 2;

/*CBoard*/
/**
 * TickTacToe root namespace
 */
if (typeof TickTacToe == "undefined" || !TickTacToe)
{
    var TickTacToe = {};
}

(function (){
    TickTacToe.CBroad = function()
    {
        this.value = [];
    };

    TickTacToe.CBroad.prototype = {
        isEmpty: function(x, y){
            if (x > 4 || y > 4 ||
                x < 0 || y < 0)
                return false;
            return this.value[x][y] == EMPTY_VALUE;
        },
        isCompleted: function() {
            return false;
        },
        check: function (x, y, value){
            if (this.isEmpty(x, y)){
                this.value[x][y] = value;
                return true;
            }
            return false;
        },
        init: function () {
            this.value = new Array(5);
            for (var i = 0; i < 5; i++){
                this.value = new Array(5);
            }

            for (i = 0; i < 5; i++){
                for (var j = 0; j < 5; j++){
                    this.value[i][j] = EMPTY_VALUE;
                }
            }
        }
    };
})();

/*CPlayer*/
 (function (){
     TickTacToe.CPlayer = function(ID){
        this.id = ID;
     }
 })();

/*CGame Function*/
(function(){
    TickTacToe.CGame = function(game){
        this.game = game;
        TickTacToe.Main.superclass.constructor.call(this, true);
        console.log('Game Width: ' + game.width);
        console.log('Game Height: ' + game.height);
        this.isOver = false;
        this.curPlayer = 0;
        this.board = new TickTacToe.CBroad();
        this.player1 = new TickTacToe.CPlayer(0);
        this.player2 = new TickTacToe.CPlayer(1);
    };

    extend(TickTacToe.CGame, Game.Scene,
     {  game: null,
        onInitScene: function onInitScene (){
            this.board.init();
        },
        onRenderScene: function onRenderScene (ctx){
            ctx.drawImage(g_oImage, 300, 440);

            var xStart = 0;
            var yStart = 0;
            for (yStart = 0;  yStart < GameHandler.height; yStart += 30){
                 xStart = 0;
                for (; xStart < GameHandler.width; xStart += 30){
                    ctx.drawImage(g_bkImage, xStart, yStart);
                }
            }
        },
        onKeyDownHandler: function onKeyDownHandler(keyCode)
        {
        },

        onKeyUpHandler: function onKeyUpHandler(keyCode)
        {
        },

        mouseToMap: function mouseToMap(x, y){

        },

      isComplete: function isComplete()
        {
            return this.board.isCompleted();
        },

        nextPlayer: function() {
            this.curPlayer = (this.curPlayer + 1) % 2;
        },

        isTurn: function (playerId) {
            return (playerId == this.curPlayer);
        },

        move: function (x, y, playerId){
            return this.board.check(x, y, playerId);
        }
    });
})();

/**
 * Loading screen scene class
 *
 * @namespace Ticktactoe
 * class Ticktactoe.Loading Scene
 */
var loadingScreen;
(function()
{
    TickTacToe.LoadingScene = function (game)
    {
        this.game = game;

        // allow start via mouse click - useful for testing on touch devices
        loadingScreen = this;

        TickTacToe.LoadingScene.superclass.constructor(this, false);
    };

    extend(TickTacToe.LoadingScene, Game.Scene, {
        imagesLoaded: false,
        game: null,
        onInitScene: function onInitScene (){

        },
        onRenderScene: function onRenderScene (ctx){
            if (!this.imagesLoaded)
            {
                Graphics.centerDrawText(ctx, "Please wait... Loading Images...", "18pt Courier New", GameHandler.height*0.5, "black");
            }
        },
        onKeyDownHandler: function onKeyDownHandler(keyCode)
        {
        },

        onKeyUpHandler: function onKeyUpHandler(keyCode)
        {
        },

        isComplete: function isComplete()
        {
            return this.imagesLoaded;
        },

        Ready: function Ready ()
        {
            this.game.curScene = new TickTacToe.CGame(this.game);
            this.imagesLoaded = true;
            // switch scene for other action

        }
    });
})();